import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.JFrame;


class Visualize extends Canvas implements Runnable
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3586224636650220903L;
	
	protected Dimension size;
	public Vector particles;
	
	private Particles p;
	private Thread allnew;
	

	Color pcolor;
	
	BufferedImage buffer;
	Graphics gbuffer;
	
	private boolean allowConn = true;
	static int rangeThreshold = 50;

	Visualize(Dimension d)
	{
		// size = new Dimension(d);
		size = d;
		particles = new Vector();
		p = new Particles();
		
		buffer = new BufferedImage(size.height,size.height,BufferedImage.TYPE_INT_RGB);
		gbuffer = buffer.getGraphics();
		
		allnew = new Thread(this);
		allnew.start();
		
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void paint (Graphics g) 
	{
		gbuffer.setColor(new Color(0,0,0));
		gbuffer.fillRect(0,0,size.width, size.height);
		
		
		for(int i = 0; i<particles.size(); i++)
		{
			p = (Particles) particles.get(i);
			int newcol = p.position.z%110+140;
			
			pcolor = new Color(255,newcol,0);
			gbuffer.setColor(pcolor);
			
			gbuffer.fillRect(p.position.width,p.position.height,3,3);
			
			
			if(allowConn)
			{
				for(int j = (i + 1); j<particles.size(); j++)
				{
					gbuffer.setColor(new Color(255,30,0));
					Particles p2 = new Particles();
					p2 = (Particles) particles.get(j);
					
				
					if(rangeThreshold>Math.abs(p.position.width-p2.position.width) && rangeThreshold>Math.abs(p.position.height-p2.position.height))
						gbuffer.drawLine(p.position.width,p.position.height,p2.position.width,p2.position.height);
				}
			}
			
		}
		
		g.drawImage(buffer, 0, 0, size.width,size.height, this);
	}

	public void run()
	{
		while(true)
		{
			repaint();	
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
	}
	
	
}

public class ParticleMaster 
{
	@SuppressWarnings("unchecked")
	
	public static void main(String[] args) 
	{
		Toolkit t = Toolkit.getDefaultToolkit();
		Dimension d = t.getScreenSize();
		
		TreeDimension border = new TreeDimension(d,100);
		
		//TreeDimension border = new TreeDimension(300,300,100);
		
		JFrame win = new JFrame();
		win.setTitle("Image");
		win.setExtendedState(JFrame.MAXIMIZED_BOTH);
		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		win.setSize(border);
		
		Visualize m = new Visualize(border);
		
		win.add(m);
		
		
		for(int i=0; i<100; i++)
		{
			m.particles.add(new Particles(border, new TreeDimension((int)(Math.random()*border.width),(int)(Math.random()*border.height),100)));
		}
		
		
		
		
		win.setVisible(true);
	
	}

}
